﻿//alert('wat?');

document.addEventListener("mousedown", function(event){
//right click
if(event.button == 2) {
	
	if((document.location.href.indexOf('showthread') > -1) && (event.target.href) && (event.target.href.indexOf('u=') > -1)) {
        chrome.extension.sendRequest({cmd: "createUserMenu", userid: event.target.href.split('u=')[1], user_name: event.target.innerHTML, gameTag: gameTag, thread: thread});
		
	} else if((document.location.href.indexOf('showthread') > -1) && (event.target.src) && (event.target.parentNode.href) && (event.target.parentNode.href.indexOf('u=') > -1))
	{
		chrome.extension.sendRequest({cmd: "createAvatarMenu", userid: event.target.parentNode.href.split('u=')[1], avatar_label: event.target.alt, gameTag: gameTag});
    } else if( document.location.href.indexOf('showthread') > -1) {
        chrome.extension.sendRequest({cmd: "createRegularMenu", thread: thread, gameTag: gameTag});
    } else {
		chrome.extension.sendRequest({cmd: "zeroMenu"});
	}
	
	//*/
   }
}, true); 


if (window.top != window.self)  //-- Don't run on frames or iframes
    return;

// Xpath Helper function copied from greasemonkey wiki
function $x() {
  var x='';
  var node=document;
  var type=0;
  var fix=true;
  var i=0;
  var cur;
    
  function toArray(xp) {
    var final=[], next;
    while (next=xp.iterateNext()) {
      final.push(next);
    }
    return final;
  }
  
  while (cur=arguments[i++]) {
    switch (typeof cur) {
      case "string": x+=(x=='') ? cur : " | " + cur; continue;
      case "number": type=cur; continue;
      case "object": node=cur; continue;
      case "boolean": fix=cur; continue;
    }
  }
  
  if (fix) {
    if (type==6) type=4;
    if (type==7) type=5;
  }
  
  // selection mistake helper
  if (!/^\//.test(x)) x="//"+x;

  // context mistake helper
  if (node!=document && !/^\./.test(x)) x="."+x;

  var result=document.evaluate(x, node, null, type, null);
  if (fix) {
    // automatically return special type
    switch (type) {
      case 1: return result.numberValue;
      case 2: return result.stringValue;
      case 3: return result.booleanValue;
      case 8:
      case 9: return result.singleNodeValue;
    }
  }

  return fix ? toArray(result) : result;
}
	
//var thread = document.location.toString().split("t=")[1].split("&")[0];
//Getting the threadid from the location string doesn't work when the user views a thread by postid, changing to the "action" value of the form element with id "inlinemodform" as this will always contain the threadid.
//var thread = document.getElementById('inlinemodform').action.toString().split("threadid=")[1].split("&")[0];

//One of these two methods should be able to identify the current thread regardless of the URL used to display it...
if (document.location.href.indexOf('showthread') > -1) {
var showthreadlinks = $x("//a[contains(@href, '/forums/showthread.php?')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
var hiddenoptions = $x("//input[contains(@name, 'threadid')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
if (showthreadlinks[0].href.split('?t=')[1]) {
	var thread = showthreadlinks[0].href.split('t=')[1].split('&')[0];
} else  if (hiddenoptions) {
	var thread = hiddenoptions[0].value
} 

document.getElementsByTagName('td')[0].innerHTML+= "<div id='orp_system_message_div' style='position:fixed;top:0px;right:0px;'><!-- ORP System Messages --></div>"
}

//alert(thread.toString());
var currentGame = -1;
var foundCurrent = false;
var foundThread = false;
//var players = new Array();
var s = "";
var myPrefs = "";
var hide_user = 0;
var gameTag = "";
var show_label = 0;
var mod_images = 0;
var settings_object = new Object();
var cast = new Object();
chrome.extension.sendRequest({method: "read_settings"}, function(response) {
  //console.log(response.settings_string);
  s = response.settings_object.cached_settings_string;
  hide_user = response.settings_object.hide_user;
  show_label = response.settings_object.show_label;
  mod_images = response.settings_object.mod_images;
  settings_object = response.settings_object;
  //console.log(response.settings_string);
  //console.log(response.hide_user);
  myPrefs = s.split(';');
  Proceed();
});

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
	//console.log("Request Received:");
	//console.log(request);
    if (request.method == "make_page_dirty") {
	  var orp_system_message_div = document.getElementById('orp_system_message_div');
	  settings_object = request.settings_object;
	  console.log(settings_object);

		soft_find();
		if (foundThread && !cast.players)
		{
			soft_cast();
		} else if (foundThread==false) {
			delete cast.players;
		}

	  if (settings_object.on_update == 3) {
		//Dirty
		orp_system_message_div.innerHTML += "<span style='color: red'>"+request.message+"</span><br />"
	  } else if (settings_object.on_update == 2) { 
		//Soft
		if (cast.players) {
			soft_write ();
		} else {
			// soft update not supported when thread not added to a game (I can fix this by generating the cast object on all pages)
			orp_system_message_div.innerHTML += "<span style='color: red'>"+request.message+"</span><br />";
		}
	  } else if (settings_object.on_update == 1) {
		//Hard
		document.location.reload();
	  } else {
		//None
	  }
	  sendResponse({answer: "page dirtied with message: "+ request.message});
	} else if (request.method == "get_page_data") {
	  var returningobject = new Object();
	  returningobject.thread = thread;
	  returningobject.gameTag = gameTag;
	  returningobject.cast = cast;
	  sendResponse({answer: "page data loaded", returningobject: returningobject});
    } else {
      sendResponse({}); // snub them.
	}

});




//console.log(myPrefs);
//alert(myPrefs);
function Proceed(){
//Stuff to Do on everypage

//cribsheet is experimental.
if (settings_object.cribsheet == 1) {
do_add_cribsheet();
}
//do_add_postsmash(); //postsmash is broken.

if (settings_object.alter_postcount_links == 1 ){ do_alter_postcount_links();}
console.log(settings_object);
for (i=0; i<myPrefs.length; i++)
{	
	pair = myPrefs[i].split('=');
	//alert(pair);
	//pair = item.split('=');
	//alert(pair[0]+" - "+pair[1]);
	if (pair[0]=='GAME')
		{
			currentGame = pair[1];
			lastGameTag = currentGame;
			foundCurrent = false;
		}
	if (pair[0]=='THREAD')
		{
			if (pair[1] == thread)
			{
				gameTag = lastGameTag;
				foundCurrent = true;
				foundThread = true;
			}
		}
	//if ((pair[0]=='PLAYER') & (foundCurrent == true))
	//	{
	//		players.push(pair[1].split(':'));
	//		//alert(player);
	//		//players[player[0]] = player[1];
	//	}
}


window.setTimeout( function() 
{
if (foundThread == true)
{
	//console.log(settings_object);
	var navbars = $x("//*[contains(@class, 'navbar')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
	//console.log(navbars);
	
	//console.log(settings_object);
	var layout = 0; //1 for Vertical
	
	var users = $x("//a[contains(@class, 'bigusername')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);

	if (settings_object.hide_joindate || settings_object.hide_memberno || settings_object.hide_location || settings_object.hide_poststats) {
		var all_smallfont_divs = $x("//div[contains(@class, 'smallfont')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
		//console.log(all_smallfont_divs.length);
		for (var i=0; i< all_smallfont_divs.length; i++) {
			var currentDivs = all_smallfont_divs[i].getElementsByTagName('div');
			//console.log(currentDivs);
			if (settings_object.hide_joindate==1 && currentDivs[0] && currentDivs[0].innerHTML.indexOf("Join Date:") > -1) {currentDivs[0].innerHTML = "<!--"+currentDivs[0].innerHTML+"-->";};
			if (settings_object.hide_memberno==1 && currentDivs[1] && currentDivs[1].innerHTML.indexOf("Member:") > -1) {currentDivs[1].innerHTML = "<!--"+currentDivs[1].innerHTML+"-->";};
			if (settings_object.hide_location==1 && currentDivs[2] && currentDivs[2].innerHTML.indexOf("Location:") > -1) {currentDivs[2].innerHTML = "<!--"+currentDivs[2].innerHTML+"-->";};
			if (settings_object.hide_poststats==1 && currentDivs[2] && currentDivs[2].innerHTML.indexOf("Posts:") > -1) {currentDivs[2].innerHTML = "<!--"+currentDivs[2].innerHTML+"-->";};
			if (settings_object.hide_poststats==1 && currentDivs[3] && currentDivs[3].innerHTML.indexOf("Posts:") > -1) {currentDivs[3].innerHTML = "<!--"+currentDivs[3].innerHTML+"-->";};
			if (settings_object.hide_status_br==1 && all_smallfont_divs[i].firstChild.nodeName == "BR") {
				all_smallfont_divs[i].removeChild(all_smallfont_divs[i].firstChild);
			}
		}
	}
	for (var i = 0; i < users.length; i++)
		{
			var thisUser = users[i];
			
			
			post_id = thisUser.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id.split('post')[1];
			player_id = thisUser.href.split('u=')[1];
			if (settings_object.cribsheet > 0 ) {
				write_cribsheet(post_id,gameTag,player_id);
			}
			
			//write_postsmash(post_id);
			if (mod_images == 2) {
				var tds = thisUser.parentNode.parentNode.parentNode.getElementsByTagName('td')
				if (tds.length > 2) {
					// Horizontal Layout
					if (tds[0].className  != 'alt2') {
						new_cell = tds[0].parentNode.insertCell(0);
						new_cell.className = "alt2";
						new_cell.innerHTML = '<a href="' + thisUser.href + '"><img src="http://www.gravatar.com/avatar/0000000'+thisUser.href.split('u=')[1]+'?s=60&d=retro" width="60" height="60" border="0" title="'+thisUser.innerHTML+'\'s Avatar"></a>';
					} else {
						//
					};
				} else if (tds.length <= 2) {
					layout = 1;
					// Vertical Layout
					//console.log(tds[0].getElementsByTagName('div')[2].innerHTML.indexOf('<img'));
					if (tds[0].getElementsByTagName('div')[2].innerHTML.indexOf('<img') == -1) {
						tds[0].getElementsByTagName('div')[2].innerHTML += "&nbsp;<br><a href='" + thisUser.href + "'><img src='http://www.gravatar.com/avatar/0000000"+thisUser.href.split('u=')[1]+"?s=60&d=retro' width='60' height='60' border='0' title=\""+thisUser.innerHTML+"\'s Avatar\"></a>";
					}
					
				}
			} else if (mod_images == 1) {
				var thisElement = thisUser.parentNode.parentNode.parentNode.getElementsByTagName('td')[0];
				if (thisElement.className  == 'alt2') {
					thisElement.parentNode.removeChild(thisElement);
				} else {
					//
				};
				
			}
		}
		var new_rule="img.the_avatar {";
		if (settings_object.custom_avatar_height > 0 ) {
			new_rule+="max-height: "+settings_object.custom_avatar_height+"px;";
		}
		if (settings_object.custom_avatar_width > 0 ) {				
			new_rule+="max-width: "+settings_object.custom_avatar_width+"px;";
		}
		new_rule+="}";
		var head = document.getElementsByTagName('head')[0],
			style = document.createElement('style'),
			rules = document.createTextNode(new_rule);
		style.type = 'text/css';
		if(style.styleSheet)
			style.styleSheet.cssText = rules.nodeValue;
		else style.appendChild(rules);
		head.appendChild(style);
	var users = $x("//a[contains(@class, 'bigusername')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
	for (var i = 0; i < users.length; i++) {
		if (mod_images == 2) {
			var this_userid = users[i].href.split('member.php?u=');
			if ((this_userid.length > 1) && (settings_object.games[gameTag].players[this_userid[1]]) && (settings_object.games[gameTag].players[this_userid[1]].character_image_url)) {
				var img_node = users[i].parentNode.parentNode.parentNode.getElementsByTagName('img')[0];
				img_node.src = settings_object.games[gameTag].players[this_userid[1]].character_image_url;
				//var custom_avatar_height = -1;
				//var custom_avatar_width = -1;
				//console.log(img_node.style);
				//if (!img_node.style) { img_node.style = ""};
				//console.log(document.styleSheets[0].cssRules);

				//console.log(document);
				var new_rule="img.the_avatar {";
				if (settings_object.custom_avatar_height < 0 ) {
					img_node.removeAttribute("height",0);
				} else if (settings_object.custom_avatar_height > 0) {
					img_node.removeAttribute("height",0);
					img_node.className = "the_avatar";
				}
				if (settings_object.custom_avatar_width < 0 ) {				
					img_node.removeAttribute("width",0);
				} else if (settings_object.custom_avatar_width > 0) {
					img_node.removeAttribute("width",0);
					img_node.className = "the_avatar";
				}
			}
		}
		
		
	}
	if (show_label == 1) {
		document.getElementsByTagName('td')[0].innerHTML+= "<div id='title_div' style='position:fixed;left:0px;bottom:0px;-webkit-transform-origin: 0% 0%; -webkit-transform: rotate(-270deg) scaleX(-1) scaleY(-1);'>"+gameTag+"</div>"
	} else {
		document.getElementsByTagName('td')[0].innerHTML+= "<div id='title_div' style='position:fixed;left:0px;bottom:0px;-webkit-transform-origin: 0% 0%; -webkit-transform: rotate(-270deg) scaleX(-1) scaleY(-1);'><!--"+gameTag+"--></div>"
	}
	
	
	
	
	users = $x("//a[contains(@class, 'bigusername')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
	cast.players = new Object();
	for (player in settings_object.games[gameTag].players) {
		cast.players[player] = new Object();
		cast.players[player].user="unknown";
		cast.players[player].title="unknown";
		cast.players[player].character=settings_object.games[gameTag].players[player];
		cast.players[player].found=false;
	}
		for (var i = 0; i < users.length; i++)
		{

			//console.log();
			var thisUser = users[i];
			if (!cast.players[thisUser.href.split('u=')[1]]) {
				cast.players[thisUser.href.split('u=')[1]] = new Object();
				cast.players[thisUser.href.split('u=')[1]].user=thisUser.innerHTML;
				cast.players[thisUser.href.split('u=')[1]].title = thisUser.parentNode.parentNode.getElementsByTagName('div')[1].innerHTML;
				cast.players[thisUser.href.split('u=')[1]].character=settings_object.games[gameTag].players[thisUser.href.split('u=')[1]];
				cast.players[thisUser.href.split('u=')[1]].found=true;
			} else {
				cast.players[thisUser.href.split('u=')[1]].user=thisUser.innerHTML;
				cast.players[thisUser.href.split('u=')[1]].title = thisUser.parentNode.parentNode.getElementsByTagName('div')[1].innerHTML;
				cast.players[thisUser.href.split('u=')[1]].found=true;
			}
			for (player in settings_object.games[gameTag].players) {
				
				if (!cast.players[player]) {
					cast.players[player] = new Object();
					cast.players[player].user=thisUser.innerHTML;
					cast.players[player].character=settings_object.games[gameTag].players[thisUser.href.split('u=')[1]];
				}
			
			
				if (thisUser.href.split('u=')[1] == player)
					{
						if (hide_user == 1 && settings_object.games[gameTag].players[player].character_tag) {
							thisUser.innerHTML = settings_object.games[gameTag].players[player].character_tag;
						} else if (settings_object.games[gameTag].players[player].character_tag){
							thisUser.innerHTML = thisUser.innerHTML + " - " + settings_object.games[gameTag].players[player].character_tag;
						}
						var detail = thisUser.parentNode.parentNode.getElementsByTagName('div')[1];
						var character_detail = settings_object.games[gameTag].players[player].character_detail;
						if (settings_object.show_detail == 3 && character_detail) {
							// Replace
							detail.innerHTML = character_detail;
						} else if (settings_object.show_detail == 2 && character_detail) {
							// Add
							detail.innerHTML += " - "+character_detail;
						} else if (settings_object.show_detail == 1) {
							// Remove
							detail.innerHTML = "<!--"+detail.innerHTML +" - "+character_detail+"-->";
						} else {
							// Leave Alone
							detail.innerHTML += "<!--"+character_detail+"-->";
						};
						//var show_sheetlink = 1; // this will come from settings_object.show_sheetlink when the setting is made optional
						if (settings_object.show_sheetlink == 1) {
							var container
							if (layout == 1) {
								container = thisUser.parentNode.parentNode.parentNode.parentNode.childNodes[0].childNodes[3];
								//console.log(container);
							 } else {
								container = thisUser.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.childNodes[0].childNodes[1].childNodes[1];
							 };
							var sheet_thread = settings_object.games[gameTag].players[player].sheet_thread;
							if (sheet_thread) {
								container.innerHTML = "<a href='showthread.php?t="+sheet_thread+"' target='_new' class='yes_sheetlink'>Character Sheet</a>"+container.innerHTML;
							} else {
								container.innerHTML = "<a href='#'  target='_new' class='no_sheetlink'>No Character Sheet</a>"+container.innerHTML;
								
							}
							
						} else {
							// do nothing... but maybe we could do something...
						}
					}
			}
			
		}
		//console.log(cast);


}
}, 100);}
//*/

//
function do_alter_postcount_links() {
	/* postcount links are a link to a view of a specific post, these seems useless to me so this chunk of code changes
	<a href="showpost.php?p=406018&postcount=589" target="new" rel="nofollow" id="postcount406018" name="589"><strong>589</strong></a>
	to
	<a href="showthread.php?p=406018" target="_new" rel="nofollow" id="postcount406018" name="589"><strong>589</strong></a>
	*/
	postcount_links = $x("//a[contains(@id, 'postcount')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
	for (item in postcount_links) {
		postcount_links[item].target = "_new";
		postcount_links[item].href = "showthread.php?p="+postcount_links[item].href.split('?p=')[1].split('&')[0];
	}
}



function do_add_cribsheet() {
	posts = $x("//table[contains(@class, 'tborder') and starts-with(@id, 'post')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
	for (item in posts) {
		//console.log(posts[item]);
		new_cell = posts[item].getElementsByTagName('tr')[0].insertCell(-1);
		new_cell.className = "alt1";
		new_cell.id = "cribsheet"+posts[item].id.split('post')[1];
		new_cell.rowSpan = "3";
		new_cell.width = '200px';
		new_cell.style.verticalAlign = 'top';

	}
}

function write_cribsheet(postId, game, player) {
	cribsheet = document.getElementById('cribsheet'+postId);
	forum_header = document.getElementById('post'+postId).getElementsByTagName('table')[0];
	cribsheet.appendChild(forum_header.parentNode.removeChild(forum_header));
	if (thisPlayer = settings_object.games[game].players[player]) {
	
		basic_info_div = document.createElement('div');
		basic_info_div.className = "smallfont"
		basic_info_div.innerHTML = "<div style='float: left'><img class='the_avatar'  src='"+thisPlayer.character_image_url+"' /></div>"
		basic_info_div.innerHTML += "<div>"+thisPlayer.character_tag+"</div>"
		basic_info_div.innerHTML += "<div>"+thisPlayer.character_detail+"</div>"
		basic_info_div.innerHTML += "<div><a href='http://online-roleplaying.com/forums/showthread.php?t="+thisPlayer.sheet_thread+"' target='_new'>Character Sheet</a></div>"
		cribsheet_div = document.createElement('div');
		cribsheet_div.style.cssClear = "both";
		cribsheet_div.innerHTML = thisPlayer.crib_sheet;
		
		cribsheet.appendChild(basic_info_div);
		cribsheet.appendChild(cribsheet_div);
	}
	
	
	
}



function do_add_postsmash() {
	posts = $x("//table[contains(@class, 'tborder') and starts-with(@id, 'post')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
	for (item in posts) {
		//console.log(posts[item]);
		//posts[item].getElementsByTagName('tr')[0].firstChild.width = "200px";
		//posts[item].getElementsByTagName('tr')[1].firstChild.width = "200px";
		new_cell = posts[item].getElementsByTagName('tr')[0].insertCell(1);
		new_cell.className = "alt1";
		new_cell.id = "postsmash"+posts[item].id.split('post')[1];
		new_cell.rowSpan = "3";
		new_cell.width = '1100px';
		new_cell.style.verticalAlign = 'top';

	}
}

function write_postsmash(postId) {
	postsmash = document.getElementById('postsmash'+postId);
	post_body = document.getElementById('post_message_'+postId)
	postsmash.appendChild(post_body.parentNode.removeChild(post_body));
}

// soft_find, soft_cast, and soft_write update the page after the settings object has been modified
function soft_find () {
	foundThread = false;
	gameTag = "";
	for (game in settings_object.games)
	{	
		if (settings_object.games[game].threads[thread])
			{
				foundThread = true;
				gameTag = game;
				console.log('soft_find () Thread Found');
			}
	}
}

function soft_cast () {

	cast.players = new Object();
	//Get the Cast from the Game
	for (player in settings_object.games[gameTag].players) {
		cast.players[player] = new Object();
		cast.players[player].user="unknown";
		cast.players[player].title="unknown";
		cast.players[player].character=settings_object.games[gameTag].players[player];
		cast.players[player].found=false;
	}
	//Loop through the posts
	posts = $x("//a[contains(@class, 'bigusername')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
	for (i in posts)
	{
		var thisPost = posts[i];
		if (!cast.players[thisPost.href.split('u=')[1]]) {
			cast.players[thisPost.href.split('u=')[1]] = new Object();
			cast.players[thisPost.href.split('u=')[1]].user=thisPost.innerHTML;
			cast.players[thisPost.href.split('u=')[1]].title = thisPost.parentNode.parentNode.getElementsByTagName('div')[1].innerHTML;
			cast.players[thisPost.href.split('u=')[1]].character=settings_object.games[gameTag].players[thisPost.href.split('u=')[1]];
			cast.players[thisPost.href.split('u=')[1]].found=true;
		} else {
			cast.players[thisPost.href.split('u=')[1]].user=thisPost.innerHTML;
			cast.players[thisPost.href.split('u=')[1]].title = thisPost.parentNode.parentNode.getElementsByTagName('div')[1].innerHTML;
			cast.players[thisPost.href.split('u=')[1]].found=true;
		}
		for (player in settings_object.games[gameTag].players) {
			
			if (!cast.players[player]) {
				cast.players[player] = new Object();
				cast.players[player].user=thisPost.innerHTML;
				cast.players[player].character=settings_object.games[gameTag].players[thisPost.href.split('u=')[1]];
			}
		
		}
		
	}
}

function soft_write () {
	var layout = 0; //1 for Vertical
	
	var users = $x("//a[contains(@class, 'bigusername')]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);

	for (var i = 0; i < users.length; i++) {
		if (mod_images == 2) {
			var thisUser = users[i];
			var this_userid = users[i].href.split('member.php?u=');

			if ((this_userid.length > 1) && (settings_object.games[gameTag].players[this_userid[1]]) && (settings_object.games[gameTag].players[this_userid[1]].character_image_url)) {
				var img_node = users[i].parentNode.parentNode.parentNode.getElementsByTagName('img')[0];
				if (!img_node) {
					var tds = thisUser.parentNode.parentNode.parentNode.getElementsByTagName('td')
					if (layout == 0) {
					// Horizontal Layout
					if (tds[0].className  != 'alt2') {
						new_cell = tds[0].parentNode.insertCell(0);
						new_cell.className = "alt2";
						new_cell.innerHTML = '<a href="' + thisUser.href + '"><img src="http://www.gravatar.com/avatar/0000000'+thisUser.href.split('u=')[1]+'?s=60&d=retro" width="60" height="60" border="0" title="'+thisUser.innerHTML+'\'s Avatar"></a>';
					};
					} else if (layout == 1) {
						// Vertical Layout
						//console.log(tds[0].getElementsByTagName('div')[2].innerHTML.indexOf('<img'));
						if (tds[0].getElementsByTagName('div')[2].innerHTML.indexOf('<img') == -1) {
							tds[0].getElementsByTagName('div')[2].innerHTML += "&nbsp;<br><a href='" + thisUser.href + "'><img src='http://www.gravatar.com/avatar/0000000"+thisUser.href.split('u=')[1]+"?s=60&d=retro' width='60' height='60' border='0' title=\""+thisUser.innerHTML+"\'s Avatar\"></a>";
						}
						
					}
					img_node = users[i].parentNode.parentNode.parentNode.getElementsByTagName('img')[0];
				}
				img_node.src = settings_object.games[gameTag].players[this_userid[1]].character_image_url;
				
				/*
							if (mod_images == 2) {
				var tds = thisUser.parentNode.parentNode.parentNode.getElementsByTagName('td')
				if (tds.length > 2) {
					// Horizontal Layout
					if (tds[0].className  != 'alt2') {
						new_cell = tds[0].parentNode.insertCell(0);
						new_cell.className = "alt2";
						new_cell.innerHTML = '<a href="' + thisUser.href + '"><img src="http://www.gravatar.com/avatar/0000000'+thisUser.href.split('u=')[1]+'?s=60&d=retro" width="60" height="60" border="0" title="'+thisUser.innerHTML+'\'s Avatar"></a>';
					} else {
						//
					};
				} else if (tds.length <= 2) {
					layout = 1;
					// Vertical Layout
					//console.log(tds[0].getElementsByTagName('div')[2].innerHTML.indexOf('<img'));
					if (tds[0].getElementsByTagName('div')[2].innerHTML.indexOf('<img') == -1) {
						tds[0].getElementsByTagName('div')[2].innerHTML += "&nbsp;<br><a href='" + thisUser.href + "'><img src='http://www.gravatar.com/avatar/0000000"+thisUser.href.split('u=')[1]+"?s=60&d=retro' width='60' height='60' border='0' title=\""+thisUser.innerHTML+"\'s Avatar\"></a>";
					}
					
				}
			} else if (mod_images == 1) {
				var thisElement = thisUser.parentNode.parentNode.parentNode.getElementsByTagName('td')[0];
				if (thisElement.className  == 'alt2') {
					thisElement.parentNode.removeChild(thisElement);
				} else {
					//
				};
				
			}
				*/
			}
		}
		
		
	}
	
	/*
		if (show_label == 1) {
		document.getElementsByTagName('td')[0].innerHTML+= "<div id='title_div' style='position:fixed;left:0px;bottom:0px;-webkit-transform-origin: 0% 0%; -webkit-transform: rotate(-270deg) scaleX(-1) scaleY(-1);'>"+gameTag+"</div>"
	} else {
		document.getElementsByTagName('td')[0].innerHTML+= "<div id='title_div' style='position:fixed;left:0px;bottom:0px;-webkit-transform-origin: 0% 0%; -webkit-transform: rotate(-270deg) scaleX(-1) scaleY(-1);'><!--"+gameTag+"--></div>"
	}
	*/
	if (!document.getElementById('title_div')) {
		document.getElementsByTagName('td')[0].innerHTML+= "<div id='title_div' style='position:fixed;left:0px;bottom:0px;-webkit-transform-origin: 0% 0%; -webkit-transform: rotate(-270deg) scaleX(-1) scaleY(-1);'></div>";
	}
	if (show_label == 1) {
		
		document.getElementById('title_div').innerHTML = gameTag;
	} else {
		document.getElementById('title_div').innerHTML = "<!--"+gameTag+"-->";
	}


	for (var i = 0; i < users.length; i++)
	{

		//console.log();
		var thisUser = users[i];
		for (player in settings_object.games[gameTag].players) {

			if (thisUser.href.split('u=')[1] == player)
				{
					if (settings_object.hide_user == 1 && settings_object.games[gameTag].players[player].character_tag) {
						thisUser.innerHTML = settings_object.games[gameTag].players[player].character_tag;
					} else if (settings_object.games[gameTag].players[player].character_tag) {
						thisUser.innerHTML = cast.players[player].user+" - "+settings_object.games[gameTag].players[player].character_tag;
					} else {
						thisUser.innerHTML = cast.players[player].user;
					};
					
					var detail = thisUser.parentNode.parentNode.getElementsByTagName('div')[1];
					var character_detail = settings_object.games[gameTag].players[player].character_detail;
					if (settings_object.show_detail == 3 && character_detail) {
						//replace
						detail.innerHTML = character_detail;
					} else if (settings_object.show_detail == 2 && character_detail) {
						//add
						detail.innerHTML = cast.players[player].title+" - "+character_detail;
					} else if (settings_object.show_detail == 2) {
						//add
						detail.innerHTML = cast.players[player].title;
					}
					

					//var show_sheetlink = 1; // this will come from settings_object.show_sheetlink when the setting is made optional
					if (settings_object.show_sheetlink == 1) {
						var container
						if (layout == 1) {
							container = thisUser.parentNode.parentNode.parentNode.parentNode.childNodes[0].childNodes[3];
							//console.log(container);
						 } else {
							container = thisUser.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.childNodes[0].childNodes[1].childNodes[1];
						 };
						var sheet_thread = settings_object.games[gameTag].players[player].sheet_thread;
						if (container.getElementsByTagName('a')[0].className.indexOf("yes_sheetlink") > -1 || container.getElementsByTagName('a')[0].className.indexOf("no_sheetlink") > -1 ) {
							
						} else {
							container.innerHTML = "<a href='#' target='_new' class='yes_sheetlink'>Unknown</a>"+container.innerHTML
						}
						if (sheet_thread>0) {
							container.getElementsByTagName('a')[0].href = "showthread.php?t="+sheet_thread;
							container.getElementsByTagName('a')[0].innerHTML = "Character Sheet";
							container.getElementsByTagName('a')[0].className = 'yes_sheetlink';
						} else {
							container.getElementsByTagName('a')[0].href = "#";
							container.getElementsByTagName('a')[0].innerHTML = "No Character Sheet";
							container.getElementsByTagName('a')[0].className = 'no_sheetlink';
						}
					}
				}
		}
	}

}
		//console.log(cast);









